const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone:'',
    code: '',
    codebtn:'发送验证码',
    disabled:false,

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  // 获取输入账号 
  phone: function (e) {
    let phone = e.detail.value;
    let reg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (!reg.test(phone)) {
      wx.showToast({
        title: '手机号码格式不正确',
        icon:"none",
        duration:2000
      })
      return false;
    }
    this.setData({
      phone: e.detail.value
    })
  },

    // 获取输入账号 
    phoneCode: function (e) {
      let code = e.detail.value;
  
      this.setData({
        code: e.detail.value
      })
    },


  //发送验证码
  sendcode(){
    let that =this;
    let phone =this.data.phone;
    var data={
      phone:phone,
      bizType:''
    }

    app.api.loginCode(data).then(rest => {
      wx.hideLoading()
      if(rest&&rest.success){   
        wx.showToast({
          title: '验证码已发送.请注意接收',
          icon:"success"
        })  
        let time = 60;
        var timers=setInterval(function () {
          time--;
          if (time>0){
            that.setData({
              codebtn:time,
              disabled:true
            });
          }else{
            that.setData({
              codebtn:'发送验证码',
              disabled:false
            });
            clearInterval(timers)
          }
        },1000)
       
    
      }else{
        wx.showToast({
          title: rest.errMsg||'发送验证码失败,请重试！',
          icon: 'none',
          duration: 2000,
          mask:true
        })
      }
    })
    .catch(r => {
      console.error(r)

      wx.showToast({
        title: '发送验证码失败,请重试！',
        icon: 'none',
        duration: 2000,
        mask:true
      })
    })

  },

  // 登录处理
  login: function (evt) {
    // console.log(evt);
    var that = this;
    let phone = evt.detail.value.phone;
    let code = evt.detail.value.code;

    if (phone=="" ){
      wx.showToast({
        title: '请填写手机号码',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    console.log("code==="+code);
    if (code=="" || isNaN(code) || code.length <4 ){
      wx.showToast({
        title: '验证码格式不正确',
        icon: 'none',
        duration: 2000
      })
      return false;
    } else {


      
      var data = {
        phone:phone,
        captcha:code
      }

      app.api.mobleLogin(data).then(rest => {
        wx.hideLoading()
      
        if(rest&&rest.success){   
        
          wx.setStorageSync('tokenId',rest.data.token);

          app.upDataUserInfo()
          app.isLogin = true
          // wx.showToast({
          //   title: '登录成功',
          //   icon:"success",
          //   success:function(){
          //       wx.switchTab({
          //         url: '/pages/mine/mine'
          //       })
          //   },  
          // })       
        }else{
          wx.showToast({
            title: rest.errMsg||'登录失败,请重试！',
            icon: 'none',
            duration: 2000,
            mask:true
          })
        }
      })
      .catch(r => {
        console.error(r)
  
        wx.showToast({
          title: '登录失败,请重试！',
          icon: 'none',
          duration: 2000,
          mask:true
        })
      })

    }
  }
})